package com.ebei.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.common.util.AuthRealm;
import com.ebei.admin.common.util.CommonUtil;
import com.ebei.admin.model.dto.OrgDTO;
import com.ebei.admin.model.entity.SysCompany;
import com.ebei.admin.service.SysCompanyService;
import com.ebei.admin.service.SysOrgService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/com")
@Api(value = "园区企业", description = "企业接口")
public class CompanyController extends BaseController {

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 分页查询企业
     *
     * @param params 参数集
     * @return 用户集合
     */
    @ApiOperation(value = "企业分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkId", value = "园区名称", required = false),
            @ApiImplicitParam(name = "inputStr", value = "input输入框", required = false)
    })
    @GetMapping("/page")
    public Result<Page> byPage(@RequestParam Map<String, Object> params) {
        CommonUtil.removeT(params);
        // 不是超管，则限制parakId必须为当前登陆人的orgId，防止查出其他园区的企业数据
        if(!AuthRealm.isSpAdmin()){
            params.put("parkId",AuthRealm.getPrincipal().getOrgId());
        }
        Page page = companyService.findCompanyListByParams(new Query<>(params));
        return new Result<>(page, Result.SUCCESS, "获取成功");
    }

    /**
     * 更新企业信息
     *
     * @param map 企业信息
     * @return Result
     */
    @ApiOperation(value = "修改企业", notes = "更新企业的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "企业的组织架构Id", required = true),
            @ApiImplicitParam(name = "companySeq", value = "企业序列号", required = false),
            @ApiImplicitParam(name = "name", value = "企业名称", required = false)
    })
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Map<String, Object> map) throws Exception {
        return companyService.update(map);
    }

    /**
     * 根据用户id查询企业
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id查询所属企业")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/byUserId")
    public Result findByUserId(@RequestParam Integer userId) {
        return companyService.findByUserId(userId);
    }

    @ApiOperation(value = "查询所有企业")
    @GetMapping("/all")
    public Result findAll() {
        EntityWrapper ew = new EntityWrapper();
        ew.eq("del_flag", CommonConstant.STATUS_NORMAL);
        ew.eq("type", CommonConstant.ORGTYPE_COMP);
        return new Result(sysOrgService.selectList(ew), Result.SUCCESS);
    }

    @ApiOperation(value = "禁用/启用企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "组织id", required = true),
            @ApiImplicitParam(name = "delFlag", value = "组织类型", required = true)
    })
    @PostMapping("/lock")
    public Result lock(@RequestBody OrgDTO orgDTO) {
        return companyService.lock(orgDTO);
    }


    @ApiOperation("根据园区id查询企业列表 隐患排查专用")
    @ApiImplicitParam(name = "parkId", value = "园区id", required = true)
    @GetMapping("/getComJsonByParkIdForYHPC")
    public Result getCompanyJsonForYHPC(@RequestParam Integer parkId){
        return companyService.getCompanyJsonForYHPC(parkId);
    }

    @ApiOperation(value = "根据园区id查企业信息 适用级联框")
    @GetMapping("/getComByParkId")
    public Result getComByParkId(@RequestParam Integer parkId){
        List<SysCompany> sysCompanyList = new ArrayList<>();
        if(AuthRealm.isSpAdmin() || AuthRealm.isGov()){
            SysCompany condition = new SysCompany();
            condition.setParkId(parkId);
            condition.setDelFlag(CommonConstant.STATUS_NORMAL);
            sysCompanyList = this.companyService.selectList(new EntityWrapper<>(condition));
        }else if(AuthRealm.isCom()){
            sysCompanyList.add(this.companyService.selectById(AuthRealm.getPrincipal().getOrgId()));
        }
        return new Result(sysCompanyList, Result.SUCCESS, "获取成功");
    }

    @ApiOperation(value = "获取所有公司(设备用)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码,默认第一页", required = false),
        @ApiImplicitParam(name = "limit", value = "每页条数,默认十条", required = false)
    })
    @GetMapping("/getAllCompanyForDevice")
    public Result<Page> getAllCompanyForDevice(@RequestParam Map<String, Object> params){
        return companyService.getAllCompanyForDevice(params);
    }
}
